package com.fanshuai2.niuke.search.dfs;

import java.util.Scanner;

/**
 * 给出4个1-10的数字，通过加减乘除运算，得到数字为24就算胜利,除法指实数除法运算,运算符仅允许出现在两个数字之间,本题对数字选取顺序无要求，但每个数字仅允许使用一次，且需考虑括号运算
 * 此题允许数字重复，如3 3 4 4为合法输入，此输入一共有两个3，但是每个数字只允许使用一次，则运算过程中两个3都被选取并进行对应的计算操作。
 */
public class Calc24 {
    static boolean allVisited(boolean[] visited) {
        boolean all = true;
        for (int i = 0; i < visited.length; i++) {
            if (!visited[i]) {
                all = false;
            }
        }
        return all;
    }
    static boolean canCalc(int[] nums) {
        int result = 0;
        boolean[] visited = new boolean[nums.length];
        return dfs(nums, visited, result);
    }

    static boolean dfs(int[] nums, boolean[] visited, int result) {
        if (allVisited(visited)) {
            return result == 24;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!visited[i]) {
                visited[i] = true;

                boolean b = dfs(nums, visited, result + nums[i]) ||
                            dfs(nums, visited, result - nums[i]) ||
                            dfs(nums, visited, result * nums[i]) ||
                            (result % nums[i] == 0 && dfs(nums, visited, result / nums[i]));
                if (b) {
                    return true;
                }

                visited[i] = false;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNext()) {
            String s = in.nextLine();
            String[] arr = s.split(" ");
            int[] nums = new int[arr.length];
            for (int i = 0; i < arr.length; i++) {
                nums[i] = Integer.parseInt(arr[i]);
            }
            System.out.println(canCalc(nums));
        }
    }
}
